<?php
namespace Admin\Controller;
use Think\Controller;
class ArticleController extends BackController {
	/**
	 * 初始化
	 */
	public function __construct() {
		parent::__construct();
	}

	/**
	 * 新闻列表
	 */
    public function index() {
		$type = C('ARTICLE');
		$class = D('article_class')->kselect(array('type'=>$type));
		$this->assign('class',$class);

		$data = D('article')->mselect(); 
		$this->assign('list',$data['list']);
		$this->assign('page',$data['page']);
		$this->display();
    }

	/**
	 * 添加、修改
	 */
	public function edit() {

		if(IS_POST) {
			if(!empty($_FILES['img']['name'])) {
				$upload = new \Think\Upload();// 实例化上传类
				$upload->maxSize   =     3145728 ;// 设置附件上传大小
				$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
				$upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
				// 上传单个文件 
				$info   =   $upload->uploadOne($_FILES['img']);
				if($_FILES['img']['name']) {
					if(!$info) {// 上传错误提示错误信息
						$this->error($upload->getError());
					}else{// 上传成功 获取上传文件信息
						$_POST['img'] =  '/Uploads/'.$info['savepath'].$info['savename'];
					}
				}
			}
			$model = D('article');
			if(!$model->create()) {
				$this->error($model->getError());
				die();
			}
			$msg = $model->msave();
			if(!empty($msg)) {
				$this->success('操作成功！',U('Article/index'));
			} else {
				$this->error('操作失败！');
			}
			die();
		}

		$articleclass = D('article_class');
		$articleclass->mselect(); 
		$class = $articleclass->grade;
		$this->assign('class',$class);
		
		$id = I('get.id','','intval');
		if(!empty($id)) {
			$info = D('article')->find($id);
			if(empty($info)) {
				die('not find this data');
			}
			$this->assign('info',$info);
		}

		$this->display();
	}

	/**
	 * 删除
	 */
	public function del() {
		$id = I('get.id','','intval');
		if(!empty($id)) {
			$msg = D('article')->delete($id);
			if(empty($msg)) {
				$this->error('操作失败！',U('Article/index'));
			} else {
				$this->success('操作成功！',U('Article/index'));
			}
		}
	}

}
